<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>

						<el-col :span="6">
							<el-form-item label="分机号" prop="extNum">
								<el-input v-model="tableData.param.extNum" placeholder="请输入分机号" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item label="企业名称" prop="companyCode">
								<el-select v-model="tableData.param.companyCode" placeholder="请选择企业名称" clearable filterable
									style="width: 100%;">
									<el-option v-for="item in tableData.companyData" :value="item.value" :label="item.label"
										:key="item.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item>
								<el-button v-if="permissionStore.isPermissionsBtn('012004001')" type="primary" @click="handleRefresh()"><el-icon>
										<Loading />
									</el-icon>加载刷新</el-button>
							</el-form-item>
						</el-col>

					</el-row>
				</el-form>

			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="分机号" align="center" prop="extNum" min-width="200px" />
				<el-table-column label="企业名称" align="center" prop="companyName" min-width="200px" />
				<el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right"
					v-if="permissionStore.isPermissionsBtn('012004002')">
					<template #default="scope">
						<el-button type="primary" link @click="handleCompanyAllot(scope.row)"
							:disabled="scope.row.companyName!=null"><el-icon>
								<EditPen />
							</el-icon>分配企业</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
		<companyAllotView ref="companyAllotViewRef" :companyData="tableData.companyData" @getList="getList">
		</companyAllotView>
	</div>
</template>
<script lang="ts">
import { phoneExtListApi,refreshPhoneExtApi} from '@/api/operationsManage/phoneExt';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { usePermissionStore } from "@/store/modules/permission";
import companyAllotView from './components/companyAllot.vue'
import { companySelectApi } from '@/api/numberManage/allocationRecords';
export default defineComponent({
	name: 'phoneExtList',
	components: { companyAllotView },
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const companyAllotViewRef = ref();
		const permissionStore = usePermissionStore()
		const state = reactive({
			tableData: {
				data: [],
				companyData: [],
				total: 0,
				loading: false,
				param: {
					pageNumber: 1,
					pageLength: 10,
					extNum: null,
				},
			},
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
			getCompanyList()
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮登录 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			phoneExtListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};
		// 分配企业
		const handleCompanyAllot = (row) => {
			companyAllotViewRef.value.openDialog(row.extNum)
		}
		// 加载，刷新
		const handleRefresh = async () => {
			await refreshPhoneExtApi()
			getList()
		}

		// 获取企业下拉
		const getCompanyList = () => {
			companySelectApi().then(res => {
				let data = res.data
				for (let key in data) {
					let obj = {
						value: key,
						label: data[key]
					}
					state.tableData.companyData.push(obj)
				}
				console.log(state.tableData.companyData);

			})
		}

		return {
			proxy,
			loading,
			queryRef,
			companyAllotViewRef,
			permissionStore,
			resetQuery,
			getList,
			handleRefresh,
			handleCompanyAllot,
			...toRefs(state),
		};
	},
});
</script>
